<template>
  <div class>
    <work-calendar :list="noWorkList" :somthingList="somthingList" @getItem="getItem" @onMonthChange="monthChange"></work-calendar>
    <el-card style="text-align:right;">
      <el-button type="primary" @click="showAdd">添加</el-button>
    </el-card>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column align="center" prop="title" label="标题"></el-table-column>
      <el-table-column align="center" prop="content" label="内容"></el-table-column>
      <el-table-column align="center" prop="status" label="是否提示" :formatter="filter1"></el-table-column>
      <el-table-column align="center" prop="warnTime" label="提示时间"></el-table-column>
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="openMessage(scope.row)">删除</el-button>
          <el-button type="primary" @click="showEdit(scope.row)" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑" :visible.sync="dialogEdit" width="500px">
      <el-form :model="editForm" :rules="editRules" ref="editForm" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="editForm.content"></el-input>
        </el-form-item>
        <el-form-item label="是否提示">
          <el-select v-model="editForm.status" placeholder="请选择">
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提示时间">
          <el-time-select v-model="editForm.warnTime" :picker-options="{
              start: '06:00',
              step: '00:10',
              end: '23:00'
            }" placeholder="选择时间"></el-time-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEdit = false">取 消</el-button>
        <el-button type="primary" @click="editOk">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="添加" :visible.sync="dialogAdd" width="500px">
      <el-form :model="addForm" :rules="editRules" ref="addForm" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="addForm.content"></el-input>
        </el-form-item>
        <el-form-item label="是否提示">
          <el-select v-model="addForm.status" placeholder="请选择">
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提示时间" v-show="addForm.status==1">
          <el-time-select v-model="addForm.warnTime" :picker-options="{
              start: '06:00',
              step: '00:10',
              end: '23:00'
            }" placeholder="选择时间"></el-time-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogAdd = false">取 消</el-button>
        <el-button type="primary" @click="addOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import workCalendar from "@/components/work-calendar/work-calendar";
import {
  workCalendarInit,
  workCalendarGetSomething,
  workCalendarSomethingEdit,
  workCalendarSomethingDel,
  workCalendarSomethingAdd
} from "./function";
export default {
  components: {
    workCalendar
  },
  data() {
    return {
      dialogEdit: false,
      dialogAdd: false,
      noWorkList: [],
      somthingList: [],
      tableData: [],
      monthChangeParams: {},
      editForm: {
        title: "",
        content: "",
        status: 0,
        warnTime: ""
      },
      addForm: {
        title: "",
        content: "",
        status: 0,
        warnTime: ""
      },
      editRules: {},
      checkedDay: {},
      checkedSomething: {}
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    openMessage(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            id: row.id,
            workCalenderId: row.workCalenderId
          };
          workCalendarSomethingDel(params).then(res => {
            if (res.retHead.code == "0000000") {
              this.$message.success("操作成功");
              this.init();
              let user = JSON.parse(sessionStorage.getItem("user"));
              let params = {
                createUserId: user.id,
                value: this.checkedDay.value
              };
              this.getSomething(params);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    showEdit(row) {
      this.dialogEdit = true;
      this.checkedSomething = row;
      for (const key in this.checkedSomething) {
        this.editForm[key] = this.checkedSomething[key];
      }
    },
    editOk() {
      this.dialogEdit = false;
      let params = {
        ...this.editForm
      };
      workCalendarSomethingEdit(params).then(res => {
        if (res.retHead.code == "0000000") {
          this.$message.success("操作成功");
          this.init();
          let user = JSON.parse(sessionStorage.getItem("user"));
          let params = {
            createUserId: user.id,
            value: this.checkedDay.value,
            realName: user.realName,
            createUserName: user.username
          };
          this.getSomething(params);
        }
      });
    },
    showAdd() {
      if (!this.checkedDay.value) {
        this.$message.error("请先选择一个日期！");
        return false;
      }
      this.dialogAdd = true;
      for (const key in this.addForm) {
        this.addForm[key] = "";
      }
    },
    addOk() {
      this.dialogAdd = false;
      let user = JSON.parse(sessionStorage.getItem("user"));
      let params = {
        ...this.addForm,
        createUserId: user.id,
        value: this.checkedDay.value,
        realName: user.realName,
        createUserName: user.username
      };
      workCalendarSomethingAdd(params).then(res => {
        if (res.retHead.code == "0000000") {
          this.$message.success("操作成功");
          this.init();

          let user = JSON.parse(sessionStorage.getItem("user"));
          let params = {
            createUserId: user.id,
            value: this.checkedDay.value
          };
          this.getSomething(params);
        }
      });
    },
    init() {
      let user = JSON.parse(sessionStorage.getItem("user"));
      let params = {};
      if (this.monthChangeParams.createUserId) {
        params = this.monthChangeParams;
      } else {
        params = {
          createUserId: user.id,
          year: new Date().getFullYear(),
          month: new Date().getMonth() + 1
        };
      }

      workCalendarInit(params).then(res => {
        if (res.retHead.code == "0000000") {
          this.noWorkList = res.retBody.sys;
          this.somthingList = res.retBody.work;
        }
      });
    },
    getItem(v) {
      this.checkedDay = v;
      if (v.params.id) {
        let user = JSON.parse(sessionStorage.getItem("user"));
        let params = {
          createUserId: user.id,
          value: v.value
        };
        this.getSomething(params);
      } else {
        this.tableData = [];
      }
    },
    getSomething(params) {
      workCalendarGetSomething(params).then(res => {
        if (res.retHead.code == "0000000") {
          this.tableData = res.retBody;
        }
      });
    },
    filter1(row, column, cellValue) {
      if (cellValue == 0) {
        return "否";
      }
      if (cellValue == 1) {
        return "是";
      }
    },
    monthChange(v) {
      let user = JSON.parse(sessionStorage.getItem("user"));
      let params = {
        createUserId: user.id,
        year: v.year,
        month: v.month + 1
      };
      this.monthChangeParams = params;
      this.tableData = [];
      this.checkedDay = {};
      this.init();
    }
  },
  created() {}
};
</script>
<style lang='scss' scoped>
</style>